﻿/// <reference path="Common.ts" />
/// <reference path="Enum.ts" />

/**样式处理*/
class Appearance
{
    // #region  公里桩号格式化
    public formatStake=(val:number, row:number=null):string=> {
        if (val === 0) return "K0";
        if (!val || isNaN(val)) return "";

        var k = Math.floor(val / 1000);
        var r = (val % 1000).toFixed(2);
        r = r.replace(".00", "");

        if (r == "0")
            return "K{0}".format(k.toString());
        else
            return "K{0}+{1}".format(k.toString(),r);
    }

    public parserStake = (val: string, row: number = null): number=>{
        if (!isNaN(Number(val))) return parseFloat(val);

        if (val==undefined || val==="") return null;

        var arr: string[] = val.split("+");
        var k: number = parseInt(arr[0].replace("K", ""))*1000;
        var r:number = 0;
        if (arr.length > 1)
        {
            r = parseFloat(arr[1].replace("+", ""));
        }

        return k + r;
    }
    //#endregion
    
    // #region  时间格式化
    public formatDate = (date: string, row :number= null):string=> {
        if (!date) return;
        return this.pattern(new Date(date), "yyyy-MM-dd ");
    }

    /** * 对Date的扩展，将 Date 转化为指定格式的String * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)
    可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) * eg: * (new
    Date()).pattern("yyyy-MM-dd hh:mm:ss.S")==> 2006-07-02 08:09:04.423      
 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04      
 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04      
 * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04      
 * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18      
 */
    private pattern(date:Date,fmt:string) {
        var o = {
            "M+": date.getMonth() + 1, //月份         
            "d+": date.getDate(), //日         
            "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时         
            "H+": date.getHours(), //小时         
            "m+": date.getMinutes(), //分         
            "s+": date.getSeconds(), //秒         
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度         
            "S": date.getMilliseconds() //毫秒         
        };
        var week = {
            "0": "/u65e5",
            "1": "/u4e00",
            "2": "/u4e8c",
            "3": "/u4e09",
            "4": "/u56db",
            "5": "/u4e94",
            "6": "/u516d"
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        if (/(E+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[date.getDay() + ""]);
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }      
    //#endregion

    
    // #region  数字格式化，2个小数
    public formatNumber = (digit: number, row: number = null): string => {
        var value = Number(digit);
        if (!value || isNaN(value)) {
            return '';
        }
        else {
            return value.toFixed(2);
        }
    }
    //#endregion

    // #region 行政区域格式化
    public loadAdmDiv=()=> {
        this._admdiv = $.ajax({ url: "/api/data/regioncode", async: false, dataType: 'json' }).responseJSON[0];
    }

    private _admdiv:any;//行政区域json
    public formatAdmdiv=(val, row)=> {
        if (!val || !this._admdiv) return "";

        var code1 = this.RecursiveAdmDiv(this._admdiv, val);
        if (code1 == null) return "";
        if (code1.id == val) return code1.text;


        var code2 = this.RecursiveAdmDiv(code1, val);
        if (code2 == null) return "";
        if (code2.id == val) return code1.text + code2.text;

        var code3 = this.RecursiveAdmDiv(code2, val);
        if (code3 == null) return "";
        if (code3.id == val) return code1.text + code2.text + code3.text;

        return val;
    }
    private RecursiveAdmDiv=(code, val)=> {
        var result = null;
        if (code.children) {
            for (var i = 1; i < code.children.length; i++) {
                if (val < code.children[i].id) {
                    result = code.children[i - 1];
                    break;
                }
            }
        }
        return result;
    }
    //#endregion
        
    // #region 货车类型格式化
    public formatCarType = (val: string, row: number): string=> {
        if (!val) return val;
        var ids: string[] = val.split(',');
        var texts: string[] = new Array<string>();
        ids.forEach(function (e) {
            var id: number = Number(e);
            var text: string;
            if (id) {
                texts.push(formatEnum(id));
            } else {
                texts.push(e);
            }
        })  

        return texts.join(',');
    }
    //#endregion

}
